package com.jf.dao.impl;

import com.jf.bean.Userinfo;
import com.jf.dao.UserinfoDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class UserinfoDaoImpl implements UserinfoDao {
    @Override
    public List<Userinfo> getPage(PageInfo pageInfo, Userinfo userinfo) {
        String sql = "select u.*,r.role_name from userinfo u left join role r on u.role_id = r.id where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(userinfo.getUsername())){
            sql+="and u.username = ? ";
            objs.add(userinfo.getUsername());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getNickname())){
            sql+="and u.nickname like concat('%',?,'%') ";
            objs.add(userinfo.getNickname());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getPhone())){
            sql+="and u.phone = ? ";
            objs.add(userinfo.getPhone());
        }
        if(userinfo.getStatus() != null){
            sql+="and u.status = ? ";
            objs.add(userinfo.getStatus());
        }
        if(userinfo.getRoleId() != null){
            sql+="and u.role_id = ? ";
            objs.add(userinfo.getRoleId());
        }
        sql+="order by u.id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Userinfo.class,sql,objs);
    }

    @Override
    public int getCount(Userinfo userinfo) {
        String sql = "select count(u.id) from userinfo u left join role r on u.role_id = r.id where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(userinfo.getUsername())){
            sql+="and u.username = ? ";
            objs.add(userinfo.getUsername());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getNickname())){
            sql+="and u.nickname like concat('%',?,'%') ";
            objs.add(userinfo.getNickname());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getPhone())){
            sql+="and u.phone = ? ";
            objs.add(userinfo.getPhone());
        }
        if(userinfo.getStatus() != null){
            sql+="and u.status = ? ";
            objs.add(userinfo.getStatus());
        }
        if(userinfo.getRoleId() != null){
            sql+="and u.role_id = ? ";
            objs.add(userinfo.getRoleId());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public Userinfo getById(int id) {
        String sql = "select * from userinfo where id = ?";
        return BaseDao.baseQueryBean(Userinfo.class,sql,id);
    }

    @Override
    public int add(Userinfo userinfo) {
        String sql = "insert into userinfo values(null,?,?,?,?,?,?)";
        return BaseDao.baseUpdate(sql,userinfo.getUsername(),userinfo.getPassword(),userinfo.getNickname(),userinfo.getPhone(),userinfo.getStatus(),userinfo.getRoleId());
    }

    @Override
    public int delete(int id) {
        String sql = "delete from userinfo where id = ?";
        return BaseDao.baseUpdate(sql,id);
    }

    @Override
    public Userinfo getByUsername(String username) {
        String sql = "select * from userinfo where username = ?";
        return BaseDao.baseQueryBean(Userinfo.class,sql,username);
    }

    @Override
    public int updateSelect(Userinfo userinfo) {
        String sql = "update userinfo set ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(userinfo.getNickname())){
            sql+="nickname = ?, ";
            objs.add(userinfo.getNickname());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getPhone())){
            sql+="phone = ?, ";
            objs.add(userinfo.getPhone());
        }
        if(MyUtils.isStringNotEmpty(userinfo.getPassword())){
            sql+="password = ?, ";
            objs.add(userinfo.getPassword());
        }
        if(userinfo.getStatus() != null){
            sql+="status = ?, ";
            objs.add(userinfo.getStatus());
        }
        if(userinfo.getRoleId() != null){
            sql+="role_id = ?, ";
            objs.add(userinfo.getRoleId());
        }
        /*获取最后一个逗号的下标*/
        int lastIndex = sql.lastIndexOf(",");
        /*截取最后一个逗号前的内容*/
        sql=sql.substring(0,lastIndex)+" ";

        sql+=" where id = ?";
        objs.add(userinfo.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from userinfo where id in (");
        for(int id :ids){
            sql.append("?,");
            objs.add(id);
        }
        int lastIndex = sql.lastIndexOf(",");
        sql = new StringBuilder(sql.substring(0, lastIndex));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public List<Userinfo> getUserinfoByRoleIds(int[] roleIds) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("select * from userinfo where role_id in (");
        for(int roleId : roleIds){
            sql.append("?,");
            objs.add(roleId);
        }
        sql=new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseQuery(Userinfo.class,sql.toString(),objs);
    }

}
